package cn.fws.meap.user.web;


import java.util.List;

import cn.fws.meap.community.service.ICreationService;
import cn.fws.meap.community.vo.CreationDetaileVo;
import cn.fws.meap.system.constants.SortCreateTime;
import cn.fws.meap.user.enums.UserIsBindEnums;
import cn.fws.meap.user.vo.UserReplyListVo;
import cn.fws.meap.web.controller.WeChatUserController;
import cn.fws.framework.utils.StringUtils;
import cn.fws.meap.user.entity.Account;
import cn.fws.meap.user.entity.Concern;
import cn.fws.meap.user.service.IAccountService;
import cn.fws.meap.user.service.IConcernService;
import cn.fws.meap.user.vo.UserDetailsVo;
import cn.fws.meap.utils.logBug.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import cn.fws.meap.user.entity.User;
import cn.fws.meap.user.service.IUserService;
import cn.fws.framework.web.contoller.BaseController;
import cn.fws.framework.web.page.PagingResult;
import cn.fws.framework.web.contoller.JsonResponse;


/**
 * 个人用户 信息操作处理
 *
 * @author chenkh
 * @date 2019-03-07
 */
@RestController
@RequestMapping("/meap/user")
@Api(tags = "个人用户 信息操作处理")
public class UserController extends BaseController{
	@Autowired
	private IUserService userService;
	@Autowired
	private IConcernService concernService;

	@Autowired
	private WeChatUserController weChatUserController;
	@Autowired
	private ICreationService creationService;
	@Autowired
	private IAccountService accountService;

/*
	*//**
	 * MongoDB测试保存
	 *//*
	@ApiOperation(value = "MongoDB测试保存")
	@PutMapping("/saveUser")
	public JsonResponse saveUser(User user){
		userService.saveUser(user);
		return JsonResponse.success("操作成功");
	}

    *//**
     * MongoDB测试删除
     *//*
    @ApiOperation(value = "MongoDB测试删除")
    @DeleteMapping("/deleteUser")
    public JsonResponse deleteUser(User user){
        userService.deleteUser(user.getNickname());
        return JsonResponse.success("操作成功");
    }


    *//**
     * MongoDB测试修改
     *//*
    @ApiOperation(value = "MongoDB测试修改")
    @PostMapping("/updateUser")
    public JsonResponse updateUser(User user){
        userService.updateUser(user);
        return JsonResponse.success("操作成功");
    }


    *//**
     * MongoDB测试查询
     *//*
    @ApiOperation(value = "MongoDB测试查询")
    @PostMapping("/getUser")
    public JsonResponse getUser(User user){
        User user1 = userService.getUser(user.getNickname());
        return JsonResponse.success("操作成功", user1);
    }*/




	//账户绑定
	@ApiOperation(value = "账户绑定")
	@PostMapping("/addUserBind")
	public JsonResponse addUserBind(@RequestParam("loginName") String loginName, @RequestParam("password") String password,@RequestParam("openid") String openid){

		String bindStatus = weChatUserController.bindWx(loginName, password, openid);
		System.out.println(bindStatus);
		if("绑定成功".equals(bindStatus)){
			userService.updateBind(openid,UserIsBindEnums.YES_BIND.getCode());
			return JsonResponse.success(bindStatus,true);
		}else {

			return JsonResponse.error(bindStatus);
		}



	}
	@ApiOperation(value = "获取url")
	@PostMapping("/getUrlForWx")
	public JsonResponse getUrlForWx(@RequestParam("openid") String openid){
		User user = userService.selectById(openid);
		if(user==null || StringUtils.isEmpty(openid)){
			return toAjax(0,"404","用户不存在");
		}else {
            System.out.println(user);
            if(UserIsBindEnums.YES_BIND.getCode().equals(user.getIsBind())){
                JsonResponse urlForWx = weChatUserController.getUrlForWx(openid);
                return JsonResponse.success("操作成功",urlForWx);
            }else{
                return JsonResponse.success("你还没有绑定账户");
            }
        }


	}
	//账户解除绑定
	@ApiOperation(value = "账户解除绑定")
	@PostMapping("/releaseBindWx")
	public JsonResponse releaseBindWx(@RequestParam("openid") String openid){

		boolean bindStatus = weChatUserController.releaseBindWx(openid);

		if(bindStatus){
			userService.updateBind(openid,UserIsBindEnums.NO_BIND.getCode());
		}

		return JsonResponse.success("操作成功",bindStatus);
	}

	/**
	 * 查询个人用户列表
	 */
	//@RequiresPermissions("meap:user:list")
	@ApiOperation(value = "查询个人用户列表")
	@PostMapping("/list")
	public PagingResult<User> list(User user){
		startPage();
		List<User> list = userService.selectList(user);
		return getPagingResult(list);
	}

	/**
	 * 新增保存个人用户
	 */
	//@RequiresPermissions("meap:user:add")
	@SysLog
	@ApiOperation(value = "新增保存个人用户")
	@ApiImplicitParam(name = "user", value = "新增保存个人用户",dataType = "User")
	@PostMapping("/add")
	public JsonResponse addSave(User user)	{

		JsonResponse insert = userService.insert(user);
		return insert;

	}

	/**
	 * 修改保存个人用户
	 */
	//@RequiresPermissions("meap:user:edit")
	@ApiOperation(value = "修改保存个人用户")
	@ApiImplicitParam(name = "user", value = "修改保存个人用户",dataType = "User")
	@PutMapping("/edit")
	public JsonResponse editSave(User user)	{
		return toAjax(userService.update(user));
	}

	/**
	 * 删除个人用户
	 */
	//@RequiresPermissions("meap:user:remove")
	@ApiOperation(value = "删除个人用户")
	@ApiImplicitParam(name = "ids", value = "根据id删除个人用户",dataType = "String")
	@DeleteMapping( "/remove")
	public JsonResponse remove(String ids)	{
		return toAjax(userService.deleteByIds(ids));
	}

	@SysLog
	@ApiOperation(value = "個人用戶")
	@PostMapping( "/getUserInformation")
	public JsonResponse getUserInformation(String selfOpenId,String itOpenId){
		if(StringUtils.isEmpty(selfOpenId)){
			return toAjax(0,"404","selfOpenId不能为空");
		}else if (StringUtils.isEmpty(itOpenId)){
			return toAjax(0,"404","itOpenId不能为空");
		}else {
			//个人用户
			UserDetailsVo userDetailsVo=userService.selectUserInformation(itOpenId);
			//用户余额
			Account account = accountService.selectById(selfOpenId);
			userDetailsVo.setAccBalance(account.getAccBalance());
			//关注的状态
			Concern concernStatus=concernService.selectConcernStatus(selfOpenId,itOpenId);
			System.out.println("-----------"+concernStatus);
			userDetailsVo.setConcernStatus(concernStatus == null ? "0" : "1");
			return JsonResponse.success("操作成功",userDetailsVo);

		}
	}

	@ApiOperation(value = "個人回复")
	@PostMapping( "/getUserReply")
	public PagingResult<UserReplyListVo> getUserReply(String openId){
		startPage();
		List<UserReplyListVo> list = userService.selectUserReplyList(openId);
		return getPagingResult(list);

	}

	@ApiOperation(value = "个人创作记录列表")
	@PostMapping("/getUserArticleIssue")
	public PagingResult<CreationDetaileVo> getUserArticleIssue(String openId){
		startPage();
		List<CreationDetaileVo> list = creationService.selectListUser(openId, SortCreateTime.createTimeDesc);
		return getPagingResult(list);
	}

	@ApiOperation(value = "个人全部文章发布和回复")
	@PostMapping("/getUserPostReply")
	public PagingResult UserPostReply(String openId){
		List list = userService.selectPostReplyList(openId);
		return getPagingResult(list);
	}



}
